var booksManager;
var addNewBooksOpen;

$(function() {
	var validData = [{ valid: 1, text: '是' }, { valid: 0, text: '否'}];
	var caseData = [];
	var classfyData = [];
	for(var i in caseList){
		var inerJson = {};
		inerJson.caseid = caseList[i].id;
		inerJson.text = caseList[i].name;
		caseData.push(inerJson);
	}
	for(var i in classfyList){
		var inerJson = {};
		inerJson.classfyid = classfyList[i].id;
		inerJson.text = classfyList[i].name;
		classfyData.push(inerJson);
	}
	booksManager = window['g'] = $("#maingrid").ligerGrid({
		height : '100%',
		url:'/v1/b/getBooks',
		pageSize : 10,
		columns : [ {
			display : '二进制码',
			name : 'brocode',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			editor: { type: 'text' },
			align : "center",
			isSort : false
		}, {
			display : '分类',
			name : 'classfyid',
			minWidth : 50,
			align : "center",
			editor: { type: 'select', data: classfyData, valueColumnName: 'classfyid' },
            render: function (item)
            {
                 for(var i in classfyData){
                	 if(item.classfyid == classfyData[i].classfyid){
                		 return classfyData[i].text;
                	 }
                 }
            },
			isSort : false
		} ,{
			display : '语言',
			name : 'language',
			minWidth : 20,
			align : "center",
			editor: { type: 'text' },
			isSort : false
		}, {
			display : '书名',
			name : 'bookname',
			minWidth : 50,
			align : "center",
			minWidth : 50,
			editor: { type: 'text'}
		}, {
			display : '书架',
			name : 'caseid',
			minWidth : 100,
			align : "center",
			editor: { type: 'select', data: caseData, valueColumnName: 'caseid' },
            render: function (item)
            {
                 for(var i in caseData){
                	 if(item.caseid == caseData[i].caseid){
                		 return caseData[i].text;
                	 }
                 }
            },
			isSort : false
		}, {
			display : '简介',
			name : 'briefdesc',
			minWidth : 100,
			align : "center",
			editor: { type: 'text' },
			isSort : false
		}, {
			display : '关键字',
			name : 'sword',
			minWidth : 60,
			align : "center",
			editor: { type: 'text' },
			isSort : false
		},{
			display : '作者',
			name : 'author',
			type : 'test',
			minWidth : 50,
			editor: { type: 'text'},
			align : "center",
			isSort : false
		},{
			display : '翻译',
			name : 'translator',
			minWidth : 60,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		},{
			display : '出版社',
			name : 'pubname',
			minWidth : 20,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		},{
			display : '价格',
			name : 'price',
			minWidth : 20,
			editor: { type: 'text' },
			align : "center",
			isSort : false
		},{
			display : 'ISBN',
			name : 'isbn',
			minWidth : 60,
			align : "center",
			editor: { type: 'text' },
			isSort : false
		},{
			display : '页数',
			name : 'length',
			minWidth : 20,
			editor: { type: 'text' },
			align : "center",
			isSort : false
		},{
			display : '出版年份',
			name : 'intime',
			minWidth : 20,
			editor: { type: 'text' },
			align : "center",
			isSort : false
		},{
			display : '入库日期',
			name : 'year',
			minWidth : 50,
			format : 'yyyy年MM月dd日',
			isSort : false,
			render : function(record, rowindex, value, column){
				var newDate = new Date(value);
				return newDate.format("yyyy年MM月dd日");
			}
		},{
			display : '借出状态',
			name : 'valid',
			minWidth : 20,
			align : "center",
			editor: { type: 'select', data: validData, valueColumnName: 'valid' },
            render: function (item)
            {
                 if (item.valid == true || parseInt(item.valid) == 1){
                	 return '是';
                 }else{
                	 return '否';
                 }
            },
			isSort : false
		}],
		rownumbers : true,
		toolbar : {
			items : [ {
				text : '保存',
				click : saveUpdateBooks,
				icon : 'modify'
			}, {
				line : true
			}, {
				text : '增加',
				click : itemBooksClick,
				icon : 'add'
			}, {
				line : true
			}]
		},
		onSelectRow: function (rowdata, rowindex)
        {
           
        },
        enabledEdit: true, 
        isScroll: false, 
        rownumbers:true
	});
	
//	jQuery("#newSex").ligerComboBox();
//	jQuery("#newValid").ligerComboBox();
//	jQuery("#newRoleid").ligerComboBox();
//    jQuery("#newBrithday").ligerDateEditor();
    
    jQuery("#pageloading").hide();
	
	jQuery("#save").click(function(){
		addNewBooksInfo();
	})
	jQuery("#reset").click(function(){
		resetNewBooksInfo();
	})
});
function getUpdate()
{
    var data = booksManager.getUpdated();
    alert(JSON.stringify(data));
    return data;
}

function saveUpdateBooks(item){
	var data = booksManager.getUpdated();
	var addData = booksManager.getAdded();
	if(addData.length > 0){
		for(var i=0;i<addData.length;i++){
			data.push(addData[i]);
		}
	}
	if(data.length == 0){
		$.ligerDialog.warn("亲，您还未做任何修改哦!");
		return;
	}
	doSaveBooks(data);
}
function doSaveBooks(data,add){
	jQuery.ajax({
		url:'/v1/b/saveOrUpdate',
		data:{json:JSON.stringify(data)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('亲,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '亲,已经保存'+data.sucessList.length+'条记录!' });
				booksManager.loadServerData({pagesize:10});
			}else{
				tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
}
function itemBooksClick(item) {
	booksManager.addRow({ 
        year: new Date(),
        valid:0
    },0);
}
function searchBooks(){
	var bookName = jQuery("#bookNameKey").val();
	if(!bookName){
		$.ligerDialog.error("请输入书名进行搜索!");
		booksManager.loadServerData({pagesize:10});
		return;
	}
	booksManager.loadServerData({bookName:bookName});
}
